package com.logos.servlet.req;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/req2")
public class RequestTest02 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // GET请求逻辑
        System.out.println("RequestTest02.doGet");
        // 1.获取所有参数的map集合
        Map<String, String[]> parameterMap = req.getParameterMap();
        // 遍历
        for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
            // 获取参数名
            String parameterName = entry.getKey();
            // 获取参数值
            String[] parameterValues = entry.getValue();
            System.out.print(parameterName + " = ");
            for (String parameterValue : parameterValues) {
                System.out.print(parameterValue+" ");
            }
            System.out.println();
        }

        System.out.println("=====================================");
        // 2.根据key获取参数值
        String[] hobbies = req.getParameterValues("hobby");
        if (hobbies != null) {
            for (String hobby : hobbies) {
                System.out.println("hobby = " + hobby);
            }
        }
        // 3.根据key获取单个参数值
        String username = req.getParameter("username");
        System.out.println("username = " + username);
        String password = req.getParameter("password");
        System.out.println("password = " + password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // POST请求逻辑
        this.doGet(req, resp);
    }
}
